package com.sc.nft.dao;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sc.nft.entity.CopyrightAuction;
import com.sc.nft.entity.CopyrightAuctionRound;
import com.sc.nft.entity.vo.CopyrightAuctionRecordUserListVO;
import com.sc.nft.entity.vo.CopyrightAuctionRoundAdminVO;
import com.sc.nft.entity.vo.CopyrightAuctionRoundUserListVO;
import com.sc.nft.entity.vo.CopyrightAuctionWinListVO;
import com.sc.nft.mapper.CopyrightAuctionRoundMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;

/**
 * @Author：LiGen
 * @Package：com.sc.nft.dao
 * @Project：sc_metago
 * @name：CopyrightAuctionDao
 * @Date：2024/9/12 17:39
 * @Filename：CopyrightAuctionDao
 */
@Slf4j
@Repository
public class CopyrightAuctionRoundDao extends ServiceImpl<CopyrightAuctionRoundMapper, CopyrightAuctionRound> {

    /**
     * 当前用户参与记录 轮次
     *
     * @param copyrightAuctionRecordId
     * @param mp
     * @return
     */
    public List<CopyrightAuctionRoundUserListVO> copyrightAuctionRoundUserList(Long copyrightAuctionRecordId,Long userId) {
        return baseMapper.copyrightAuctionRoundUserList(copyrightAuctionRecordId,userId);
    }


    /**
     * 拍卖轮数
     *
     * @param copyrightAuctionRecordId
     * @return
     */
    public List<CopyrightAuctionRoundAdminVO> copyrightAuctionRoundAdminList(Long copyrightAuctionRecordId) {
        return baseMapper.copyrightAuctionRoundAdminList(copyrightAuctionRecordId);
    }

    public List<CopyrightAuctionRound> listByCopyrightAuctionRecordId(Long recordId) {
        LambdaQueryWrapper<CopyrightAuctionRound> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(CopyrightAuctionRound::getCopyrightAuctionRecordId, recordId);
        lambdaQueryWrapper.orderByAsc(CopyrightAuctionRound::getRoundStartTime);
        return this.list(lambdaQueryWrapper);
    }

    public CopyrightAuctionRound getLastRound(Long recordId, int round) {
        LambdaQueryWrapper<CopyrightAuctionRound> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(CopyrightAuctionRound::getCopyrightAuctionRecordId,recordId);
        lambdaQueryWrapper.eq(CopyrightAuctionRound::getRound,round);
        lambdaQueryWrapper.last("limit 1");
        return getOne(lambdaQueryWrapper);
    }

    public List<CopyrightAuctionWinListVO> auctionWinList(Long id) {
        return baseMapper.auctionWinList(id);
    }

    public List<CopyrightAuctionRound> getRoundList(Long copyrightAuctionRecordId) {
        LambdaQueryWrapper<CopyrightAuctionRound> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(CopyrightAuctionRound::getCopyrightAuctionRecordId, copyrightAuctionRecordId);
        lambdaQueryWrapper.last("order by round asc");
        return list(lambdaQueryWrapper);
    }

    /**
     * 更新本轮最高价
     *
     * @param id
     * @param roundHighestPrice
     * @return
     */
    public boolean updateRoundHighestPrice(Long id, Integer roundHighestPrice) {
        LambdaUpdateWrapper<CopyrightAuctionRound> queryWrapper = new LambdaUpdateWrapper<>();
        queryWrapper.eq(CopyrightAuctionRound::getId, id);
        queryWrapper.set(CopyrightAuctionRound::getRoundHighestPrice, roundHighestPrice);
        queryWrapper.set(CopyrightAuctionRound::getFinish,Boolean.TRUE);
        return this.update(queryWrapper);
    }
}
